Skip to content

Implement topological_sort_levels#501

Open
joemalle wants to merge 1 commit into
boostorg:developfrom
joemalle:feature/topological_sort_levels
Open

Implement topological_sort_levels#501
joemalle wants to merge 1 commit into
boostorg:developfrom
joemalle:feature/topological_sort_levels

Conversation

@joemalle

@joemalle joemalle commented Jun 7, 2026

Copy link
Copy Markdown

This phab implements issue #240 . Please note that I'm not a frequent contributor, and I may have made some noob mistakes. Also, I used Claude to write this. LMK if that's a foul.

Before submitting

  • This PR targets the develop branch.
  • I searched for an existing PR or issue covering the same change.
  • My contribution is licensed under the Boost Software License 1.0.

Type of change

  • Bug fix
  • New feature or API addition
  • Refactor (no behavior change)
  • Documentation
  • Build, CI, or tooling
  • Other (specify below)

Does this PR introduce a breaking change?

  • Yes (describe migration impact below)
  • No

What this PR does

Adds functions to toposort into levels. There's a lower level function that uses a property map to output the vertex levels and a vector shorthand.

Motivation

Fixes issue #240

Testing

I added new tests. Here's what I ran

$ (cd libs/graph/test && ../../../b2 topological_sort_levels_test)
Performing configuration checks

    - default address-model    : 64-bit (cached) [1]
    - default architecture     : x86 (cached) [1]
    - symlinks supported       : yes (cached)

[1] gcc-10
...patience...
...patience...
...found 2402 targets...
...updating 4 targets...
gcc.compile.c++ ../../../bin.v2/libs/graph/test/topological_sort_levels_test.test/gcc-10/debug/x86_64/threading-multi/visibility-hidden/topological_sort_levels_test.o
gcc.link ../../../bin.v2/libs/graph/test/topological_sort_levels_test.test/gcc-10/debug/x86_64/threading-multi/visibility-hidden/topological_sort_levels_test
testing.capture-output ../../../bin.v2/libs/graph/test/topological_sort_levels_test.test/gcc-10/debug/x86_64/threading-multi/visibility-hidden/topological_sort_levels_test.run
**passed** ../../../bin.v2/libs/graph/test/topological_sort_levels_test.test/gcc-10/debug/x86_64/threading-multi/visibility-hidden/topological_sort_levels_test.test

...updated 4 targets...

Checklist

  • Existing tests pass (b2 in the test/ directory).
  • New behavior is covered by a test, or this is a docs / build / refactor change.
  • Documentation was updated if user-facing behavior changed.
  • No new compiler warnings on the platforms I built against.

@joemalle joemalle marked this pull request as ready for review June 7, 2026 16:24
@jeremy-murphy

Copy link
Copy Markdown
Collaborator

Also, I used Claude to write this. LMK if that's a foul.

It's not a foul, and it is important to note, so thank you.

@jeremy-murphy jeremy-murphy self-assigned this Jun 8, 2026
@jeremy-murphy

Copy link
Copy Markdown
Collaborator

Could you please go into more detail on:

  • why this algorithm is useful (what problems does it solve, references to literature, etc)
  • why you choose this implementation, how it relates to the existing topological sort algorithm and why, for example, is it not an enhancement of the existing algorithm?

Thanks, cheers.

@Becheler

Becheler commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Thank you @joemalle for the PR and @jeremy-murphy for the comment. Feel free to edit your own PR first message 😄
Thanks again for shipping this PR on a Sunday so quickly after I reached out 🚀

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

Compiler-warning counts vs develop (auto-generated).
PR run 27098118816 vs develop run 27120109824 (724d8f3c9a).

Job Baseline After Delta
macos (clang, 14) 75 75 0
macos (clang, 17) 70 70 0
macos (clang, 20) 70 70 0
ubuntu (clang-19, 14) 75 75 0
ubuntu (clang-19, 17) 75 75 0
ubuntu (clang-19, 20) 75 75 0
ubuntu (clang-19, 23) 75 75 0
ubuntu (gcc-14, 14) 51 51 0
ubuntu (gcc-14, 17) 51 51 0
ubuntu (gcc-14, 20) 51 51 0
ubuntu (gcc-14, 23) 51 51 0
windows_msvc_14_3 (msvc-14.3) 1226 1232 +6

@Becheler

Becheler commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Note 1 : the +6 warnings on msvc are not an error of the test or header, it's due to Boost.PropertyMap lib forcing a size_t -> unsigned -> size_t. I opened a PR on Boost.PropertyMap repo that should fix this warning (and others) : boostorg/property_map#37

Note 2 Boost.PropertyMap was community maitained, I jumped in but the CI is on fire, we should not condition the present topological_sort_levels on those 6 warnings that are not @joemalle responsibility 🥲

@joemalle

Copy link
Copy Markdown
Author

Hi, I'm happy to contribute! And thank you for investigating/fixing those warnings.

Here are some answers. I can edit the main description when we have a finalized design.

why this algorithm is useful (what problems does it solve, references to literature, etc)

Sorting into "levels" or "generations" is a common issue in scheduling. You find yourself with a DAG of jobs, and you want to know which ones can run concurrently.

I faced a similar scheduling-related issue when I posted this three years ago. I wanted to find sets of instructions that could be reordered/vectorized without affecting correctness.

While writing this answer, I learned that toposorting into levels can also be useful when drawing layered graphs e.g. when rendering DOT/graphviz files.

why you choose this implementation, how it relates to the existing topological sort algorithm and why, for example, is it not an enhancement of the existing algorithm?

We absolutely could enhance the existing DFS implementation. I'm happy to switch to that if you prefer. However, we'd have to add more branches and a small amount of extra state to track the depth. This BFS algorithm is, in my opinion, more natural since it naturally iterates through levels.

This is mentioned in the commit, but just in case, this is Kahn's algorithm (https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants